<template>
	<view>
		<!-- 搜索框 -->
		<view class="search-box">
			<uni-search-bar
				@confirm="search"
				:focus="true"
				@input="input"
				:radius="100"
				cancel-button="none"
			></uni-search-bar>
		</view>
		<!-- 搜索建议列表 -->
		<view class="suggest-list" v-if="searchResults.length !== 0">
			<view
				class="suggest-item"
				v-for="(item, index) in searchResults"
				:key="index"
				@click="goDetail(item)"
			>
				<view class="goods-name">{{ item.goods_name }}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="cleanHistory"></uni-icons>
			</view>
			<view class="history-list">
				<view
					class="history-list-tag"
					v-for="(item, index) in histories"
					:key="index"
					@click="goGoodsList(item)"
				>
					{{ item }}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			timer: null, // 防抖
			value: '',
			searchResults: [],
			historyList: [] // 搜索历史
		};
	},
	computed: {
		histories() {
			return [...this.historyList].reverse();
		}
	},
	onLoad() {
		this.historyList = JSON.parse(uni.getStorageSync('value') || '[]');
	},
	methods: {
		// 输入处理函数
		input(value) {
			// 防抖
			clearTimeout(this.timer);
			this.timer = setTimeout(() => {
				this.value = value.trim();
				console.log(this.value);
				this.getSearchList();
			}, 500);
		},
		// 获得搜索列表
		async getSearchList() {
			// 判断搜索是否为空
			if (this.value.length === 0) {
				this.searchResults = [];
				return;
			}
			const { data: res } = await uni.$http.get(
				'/api/public/v1/goods/qsearch',
				{ query: this.value }
			);
			if (res.meta.status !== 200) return uni.$showMsg();
			this.searchResults = res.message;
			console.log(res);
			this.saveSearchHistory();
		},
		// 保存搜索历史
		saveSearchHistory() {
			// this.historyList.push(this.value)
			const set = new Set(this.historyList);
			// 要先删除
			set.delete(this.value);
			set.add(this.value);
			this.historyList = Array.from(set);
			// 存储历史搜索到本地
			uni.setStorageSync('value', JSON.stringify(this.historyList));
		},
		// 清空搜索历史
		cleanHistory() {
			this.historyList = [];
			uni.setStorageSync('value', '');
		},
		// 跳转商品详情
		goDetail(item) {
			uni.navigateTo({
				url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
			});
		},
		// 跳转商品列表
		goGoodsList(item) {
			uni.navigateTo({
				url: '/subpkg/goods_list/goods_list?query=' + item
			})
		},
	}
};
</script>

<style lang="scss">
/deep/ .uni-searchbar {
	background: #c00000;
}
.search-box {
	// 设置定位效果为“吸顶”
	position: sticky;
	// 吸顶的“位置”
	top: 0;
	// 提高层级，防止被轮播图覆盖
	z-index: 999;
}
.suggest-list {
	padding: 0 5px;

	.suggest-item {
		font-size: 12px;
		padding: 13px 0;
		border-bottom: 1px solid #efefef;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.goods-name {
			// 文字不允许换行（单行文本）
			white-space: nowrap;
			// 溢出部分隐藏
			overflow: hidden;
			// 文本溢出后，使用 ... 代替
			text-overflow: ellipsis;
			margin-right: 3px;
		}
	}
}
.history-box {
	padding: 0 5px;
	.history-title {
		display: flex;
		justify-content: space-between;
		height: 40px;
		align-items: center;
		font-size: 13px;
		border-bottom: 1px solid #efefef;
	}
	.history-list {
		display: flex;
		flex-wrap: wrap;
		.history-list-tag {
			margin-top: 5px;
			margin-right: 5px;
			align-items: center;
			font-size: 13px;
			padding: 4px 7px;
			color: #000000;
			border-radius: 3px;
			background-color: #f8f8f8;
		}
	}
}
</style>
